package com.mobile.liuyi_s.orderlunch.fragment;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mobile.liuyi_s.orderlunch.MyApplication;
import com.mobile.liuyi_s.orderlunch.R;
import com.mobile.liuyi_s.orderlunch.Utils.Utils;
import com.mobile.liuyi_s.orderlunch.activity.OrderListActivity;
import com.mobile.liuyi_s.orderlunch.entity.MyUser;
import com.mobile.liuyi_s.orderlunch.entity.Order;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

/**
 * A simple {@link Fragment} subclass.
 */
public class CountFragment extends Fragment implements View.OnClickListener {

    private Toolbar mToolbar;
    private MyApplication mApplication;
    private String corrUserGroup;
    private static final String TAG = "CountFragment";
    private Utils mUtils = new Utils();

    private static final int QUERY_ORDER_BY_SITE_50 = 150;
    private static final int QUERY_ORDER_BY_SITE_51 = 151;
    private static final int QUERY_ORDER_BY_SITE_52 = 152;
    private static final int QUERY_ORDER_BY_SITE_53 = 153;
    private static final int QUERY_ORDER_BY_SITE_54 = 154;

    private TextView mTextViewDate;
    private LinearLayout mLinearLayout;
    private TextView mTextView50;
    private TextView mTextView51;
    private TextView mTextView52;
    private TextView mTextView53;
    private TextView mTextView54;
    private TextView mTextViewTotal;

    private int orderNum50 = 0;
    private int orderNum51 = 0;
    private int orderNum52 = 0;
    private int orderNum53 = 0;
    private int orderNum54 = 0;

    private String defaultTitle50 = "站点：50  总份数：0";
    private String defaultTitle51 = "站点：51  总份数：0";
    private String defaultTitle52 = "站点：52  总份数：0";
    private String defaultTitle53 = "站点：53  总份数：0";
    private String defaultTitle54 = "站点：54  总份数：0";

    private TextView defaultTextView;

    public CountFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_count, container, false);
        mApplication = ((MyApplication) getActivity().getApplication());
        initView(view);
        isAdmin();

        return view;
    }

    private void initView(View itemView) {
        initToolbar(itemView);
        defaultTextView = ((TextView) itemView.findViewById(R.id.txt_default_count_fragment));
        mTextViewDate = ((TextView) itemView.findViewById(R.id.txt_default_date_count_fragment));
        mTextView50 = ((TextView) itemView.findViewById(R.id.txt_50_count_fragment));
        mTextView51 = ((TextView) itemView.findViewById(R.id.txt_51_count_fragment));
        mTextView52 = ((TextView) itemView.findViewById(R.id.txt_52_count_fragment));
        mTextView53 = ((TextView) itemView.findViewById(R.id.txt_53_count_fragment));
        mTextView54 = ((TextView) itemView.findViewById(R.id.txt_54_count_fragment));
        mTextViewTotal = ((TextView) itemView.findViewById(R.id.txt_total_count_fragment));
        mLinearLayout = ((LinearLayout) itemView.findViewById(R.id.linear_layout_count_fragment));

        mTextView50.setOnClickListener(this);
        mTextView51.setOnClickListener(this);
        mTextView52.setOnClickListener(this);
        mTextView53.setOnClickListener(this);
        mTextView54.setOnClickListener(this);
    }

    private void initToolbar(View itemView) {
        mToolbar = ((Toolbar) itemView.findViewById(R.id.toolbar_count_fragment));
        mToolbar.setTitle("");
        ((AppCompatActivity) getActivity()).setSupportActionBar(mToolbar);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        queryOrderBySite("50");
        queryOrderBySite("51");
        queryOrderBySite("52");
        queryOrderBySite("53");
        queryOrderBySite("54");
    }

    private void isAdmin() {
        MyUser user = BmobUser.getCurrentUser(MyUser.class);
        corrUserGroup = user.getUserGroup();
        Log.d(TAG, "isAdmin: " + corrUserGroup);
        if (corrUserGroup != null && corrUserGroup.equals("admin")) {
            defaultTextView.setVisibility(View.INVISIBLE);
            mTextViewDate.setText(mUtils.getLocacYear() + "年"
                    + mUtils.getLocacMonth() + "月" + mUtils.getLocacDay() + "号数据");
//            mTextView50.setText("站点：50  总份数："+String.valueOf(orderNum50));
//            mTextView51.setText("站点：51  总份数："+String.valueOf(orderNum51));
//            mTextView52.setText("站点：52  总份数："+String.valueOf(orderNum52));
//            mTextView53.setText("站点：53  总份数："+String.valueOf(orderNum53));
//            mTextView54.setText("站点：54  总份数："+String.valueOf(orderNum54));
            int totalNum = orderNum50 + orderNum51 + orderNum52 + orderNum53 + orderNum54;
            mTextViewTotal.setText("总份数：" + String.valueOf(totalNum));

        } else {
            defaultTextView.setText("您是普通用户，暂无权限查看");
            mLinearLayout.setVisibility(View.INVISIBLE);
            mTextViewDate.setVisibility(View.INVISIBLE);
        }
    }

    private void queryOrderBySite(final String site) {
        Utils utils = new Utils();
        BmobQuery<Order> querySite = new BmobQuery<>();
        querySite.addWhereEqualTo("orderSite", site);
        BmobQuery<Order> queryYear = new BmobQuery<>();
        queryYear.addWhereEqualTo("orderYear", utils.getLocacYear());
        BmobQuery<Order> queryMonth = new BmobQuery<>();
        queryMonth.addWhereEqualTo("orderMonth", utils.getLocacMonth());
        BmobQuery<Order> queryDay = new BmobQuery<>();
        queryDay.addWhereEqualTo("orderDay", utils.getLocacDay());

        ArrayList<BmobQuery<Order>> queries = new ArrayList<>();
        queries.add(querySite);
        queries.add(queryYear);
        queries.add(queryMonth);
        queries.add(queryDay);
        BmobQuery<Order> mainQuery = new BmobQuery<>();
        mainQuery.and(queries);
        mainQuery.findObjects(new FindListener<Order>() {
            @Override
            public void done(List<Order> list, BmobException e) {
                if (e == null) {
                    Log.d(TAG, "done: list大小：" + list.size());
                    if (list.size() > 0) {
                        Message message = mHandler.obtainMessage();
                        switch (site) {
                            case "50":
                                Log.d(TAG, "done: 50: " + list.size());
                                message.what = QUERY_ORDER_BY_SITE_50;
                                message.obj = list;
                                mHandler.sendMessage(message);
                                mApplication.setOrderList50(list);
                                break;
                            case "51":
                                Log.d(TAG, "done: 51: " + list.size());
                                message.what = QUERY_ORDER_BY_SITE_51;
                                message.obj = list;
                                mHandler.sendMessage(message);
                                mApplication.setOrderList51(list);
                                break;
                            case "52":
                                Log.d(TAG, "done: 52: " + list.size());
                                message.what = QUERY_ORDER_BY_SITE_52;
                                message.obj = list;
                                mHandler.sendMessage(message);
                                mApplication.setOrderList52(list);
                                break;
                            case "53":
                                Log.d(TAG, "done: 53: " + list.size());
                                message.what = QUERY_ORDER_BY_SITE_53;
                                message.obj = list;
                                mHandler.sendMessage(message);
                                mApplication.setOrderList53(list);
                                break;
                            case "54":
                                Log.d(TAG, "done: 54: " + list.size());
                                message.what = QUERY_ORDER_BY_SITE_54;
                                message.obj = list;
                                mHandler.sendMessage(message);
                                mApplication.setOrderList54(list);
                                break;
                            default:
                                break;
                        }
                    }
                }
            }
        });
    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            List<Order> orderList = (List<Order>) msg.obj;
            switch (msg.what) {
                case QUERY_ORDER_BY_SITE_50:
                    orderNum50 = mUtils.getOrderNumInList(orderList);
                    mTextView50.setText("站点：50  总份数：" + String.valueOf(orderNum50));
                    Log.d(TAG, "handleMessage: orderNumList50：" + orderNum50);
                    break;
                case QUERY_ORDER_BY_SITE_51:
                    orderNum51 = mUtils.getOrderNumInList(orderList);
                    mTextView51.setText("站点：51  总份数：" + String.valueOf(orderNum51));
                    Log.d(TAG, "handleMessage: orderNumList51：" + orderNum51);
                    break;
                case QUERY_ORDER_BY_SITE_52:
                    orderNum52 = mUtils.getOrderNumInList(orderList);
                    Log.d(TAG, "handleMessage: orderNumList52：" + orderNum52);
                    mTextView52.setText("站点：52  总份数：" + String.valueOf(orderNum52));
                    break;
                case QUERY_ORDER_BY_SITE_53:
                    orderNum53 = mUtils.getOrderNumInList(orderList);
                    Log.d(TAG, "handleMessage: orderNumList53：" + orderNum53);
                    mTextView53.setText("站点：53  总份数：" + String.valueOf(orderNum53));
                    break;
                case QUERY_ORDER_BY_SITE_54:
                    orderNum54 = mUtils.getOrderNumInList(orderList);
                    Log.d(TAG, "handleMessage: orderNumList54：" + orderNum54);
                    mTextView54.setText("站点：50  总份数：" + String.valueOf(orderNum54));
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    public void onClick(View view) {
        String site = null;
        switch (view.getId()) {
            case R.id.txt_50_count_fragment:
                Log.d(TAG, "onClick: 点击了50txt");
                if (orderNum50 != 0) {
                    site = "50";
                }
                break;
            case R.id.txt_51_count_fragment:
                Log.d(TAG, "onClick: 点击了51txt");
                if (orderNum51 != 0) {
                    site = "51";
                }
                break;
            case R.id.txt_52_count_fragment:
                Log.d(TAG, "onClick: 点击了52txt");
                if (orderNum52 != 0) {
                    site = "52";
                }
                break;
            case R.id.txt_53_count_fragment:
                Log.d(TAG, "onClick: 点击了53txt");
                if (orderNum53 != 0) {
                    site = "53";
                }
                break;
            case R.id.txt_54_count_fragment:
                Log.d(TAG, "onClick: 点击了54txt");
                if (orderNum54 != 0) {
                    site = "54";
                }
                break;
            default:
                break;
        }
        if (site != null) {
            Intent intent = new Intent(getActivity(), OrderListActivity.class);
            intent.putExtra("site", site);
            startActivity(intent);
        } else {
            Log.d(TAG, "onClick: 获取点击站点失败");
        }
    }
}
